package com.chuanjiabao.wisdom.ui.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.LinearLayout;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.adapter.IndexFamilyUsersAdapter;
import com.chuanjiabao.wisdom.util.LogUtil;
import com.chuanjiabao.wisdom.util.animation.view.ViewHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 86178 on 2019/7/24.
 * author mogen
 * 用心，成就一切。
 */

public class FuncationLinearLayout extends LinearLayout {

    Context mcontext;
    RecyclerView recyclerView;
    List<String> names = new ArrayList<>();
    List<String> namesSecond = new ArrayList<>();
    IndexFamilyUsersAdapter adapter;
    View view;

    int downX, downY, newHeight, newWidth, lastMultiTouchTime, left, top, right, bottom;
    private ScaleGestureDetector mScaleGestureDetector = null;

    public FuncationLinearLayout(Context context) {
        super(context);
        mcontext = context;
    }

    public FuncationLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mcontext = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.linear_funcation, this);
        initView();
    }

    private void initView() {

        //        names.add("李洋");
        //        recyclerView = findViewById(R.id.recycler);
        //        adapter = new IndexFamilyUsersAdapter(mcontext, names);
        //        recyclerView.setLayoutManager(new LinearLayoutManager(mcontext));
        //        recyclerView.setAdapter(adapter);

    }

    public class ScaleGestureListener implements ScaleGestureDetector.OnScaleGestureListener {
        private float scale;
        private float preScale = 1;// 默认前一次缩放比例为1

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            float previousSpan = detector.getPreviousSpan();
            float currentSpan = detector.getCurrentSpan();
            float mPrevSpanX = detector.getPreviousSpanX();
            float mPrevSpanY = detector.getPreviousSpanY();
            if (currentSpan < previousSpan) {
                // 放大
                // scale = preScale-detector.getScaleFactor()/3;
                scale = preScale - (previousSpan - currentSpan) / 1000;
                Log.e("log", "缩小-->" + scale);
                if (scale < 0.5) {
                    scale = (float) 0.5;
                }
            } else {
                // 缩小
                // scale = preScale+detector.getScaleFactor()/3;
                scale = preScale + (currentSpan - previousSpan) / 1000;
                Log.e("log", "放大-->" + scale);
                if (scale > 1) {
                    scale = (float) 1;
                }
            }

            // 缩放view
            ViewHelper.setScaleX(view, scale);// x方向上缩小
            ViewHelper.setScaleY(view, scale);// y方向上缩小
            return false;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            // 一定要返回true才会进入onScale()这个函数
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            preScale = scale;//记录本次缩放比例
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() > 1) {
            mScaleGestureDetector = new ScaleGestureDetector(mcontext,
                    new ScaleGestureListener());
            return mScaleGestureDetector.onTouchEvent(event);
        } else {
            // 单点触控
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    left = view.getLeft();
                    right = view.getRight();
                    top = view.getTop();
                    bottom = view.getBottom();

                    downX = (int) event.getX();
                    downY = (int) event.getY();
                    newHeight = view.getLayoutParams().height;
                    newWidth = view.getLayoutParams().width;
                    int widthMeasureSpec =
                            View.MeasureSpec.makeMeasureSpec(2000, View.MeasureSpec.AT_MOST);
                    int heightMeasureSpec
                            = View.MeasureSpec.makeMeasureSpec(2000, View.MeasureSpec.AT_MOST);
                    view.measure(widthMeasureSpec, heightMeasureSpec);
                    newHeight = view.getMeasuredHeight();
                    newWidth = view.getMeasuredWidth();

                    break;
                case MotionEvent.ACTION_MOVE:
                    long currentTimeMillis = System.currentTimeMillis();
                    if (currentTimeMillis - lastMultiTouchTime > 200) {
                        // 双指触控后要等待200毫秒才能执行单指触控的操作，避免双指触控后出现颤抖的情况
                        int moveX = (int) event.getX();// 移动手指的时候手指的x
                        int moveY = (int) event.getY();// 移动手指的时候手指的y
                        int deltaX = (moveX - downX);
                        int deltaY = (moveY - downY);

                        LogUtil.e("x轴移动距离-->"+ deltaX);
                        LogUtil.e("y轴移动距离-->"+ deltaY);

                        int newLeft = left + deltaX;// view的新left
                        int newTop = top + deltaY;// view的新top
                        int newRight = right + deltaX;// view的新right
                        int newBottom = bottom + deltaY;// view的新bottom

                        LogUtil.e("当前view的四个角-->" + newLeft + "/" + newTop + " /" + newRight + " /" + newBottom);
                        view.layout(newLeft, newTop, newRight, newBottom);// 重新摆放view的位置
                    } else {
                        return false;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    // 更新位置信息
                    left = view.getLeft();
                    top = view.getTop();
                    right = view.getRight();
                    bottom = view.getBottom();
                    break;
                default:
                    break;
            }
            return true;// 代表消费了事件
        }
    }
}
